// pages/goods/index.js
var api = require('../../utils/api.js')
var app = new getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH: 0,
    isIphoneX: false,
    id:'',
    content:null,
    ishow:false,
    search:"",
    activeId:"",
    activeId1:"",
    activeId2:"全部",
    activeId3:"",
    hot_sort:"",//按热度排序，0从高到低，1从低到高
    sortList:['综合推荐','按热度'],
    sortActive:0,
  },
  select(e) {
    let id = e.currentTarget.dataset.id
    let type = e.currentTarget.dataset.type
    this.setData({
      ['activeId'+type]: id
    })
    this.getData()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.setData({
        id:options.id
      })
    }else{
      let data = JSON.parse(options.item)
      this.setData({
        activeId:data.spec_id,
        activeId2:data.remark
      })
    }
    this.getData()
    this.getCountHeight()
  },
  getCountHeight(){
    let isIphoneX = app.globalData.isIphoneX
    if (isIphoneX) {
      this.setData({
        isIphoneX
      })
    }
    this.setData({
      navH: app.globalData.navHeight
    })
  },
  getData(){
    let data = this.data;
    let params = {
      spec_id:data.activeId, //规格id
      classfiy_id:data.activeId3,//系列
      craft_id:data.activeId2, //工艺id
      color_id:data.activeId1, //颜色id
      size:99999,
      page:1,
      hot_sort:this.data.hot_sort, //按热度排序，0从高到低，1从低到高
      id:this.data.id
    }
    api.post('goods_info',params).then(res => {
      if (res.code == 200) {
        if(res.data.length==0){
          api.tip("该选项下暂无相关产品")
        }
        let content = res.data
        content.goods_color_arr.unshift({id:"",title:"全部"})
        content.goods_craft_arr.unshift({title:"全部"})
        content.goods_spec_arr.unshift({id:"",title:"全部"})
        content.goods_classfiy_arr.unshift({id:"",classfiy_name:"全部"})
        this.setData({
          content
        })
      }
    })
  },
  toDetail(e){
    let id = e.currentTarget.dataset.id
    api.saveFoot(id,1)
    wx.navigateTo({
      url: './detail/detail?id='+id,
    })
  },
  changeSort(e){
    let index = e.currentTarget.dataset.index
    if(index == 0 && this.data.hot_sort === ""){
      return;
    }
    let sort 
    if(index == 0){
      sort = ''
    }else{
      sort = this.data.hot_sort === 0 ? 1 : 0
    }
    this.setData({
      sortActive : index,
      hot_sort : sort
    })
    this.getData()
  }
})